import fs from "fs";

/**
 * @获取当前时间，年月日
 * @type {String} 返回格式类型
 */
const getCurrentTime = (type) => {
  let date = new Date(),
    ts = date.getTime(),
    year = date.getFullYear(),
    month = date.getMonth() + 1,
    day = date.getDate(),
    h = date.getHours(),
    m = date.getMinutes(),
    s = date.getSeconds();

  switch (type) {
    case "YY.MM.DD":
      return year + "." + zero(month) + "." + zero(day);
    case "YY.MM.DD HH:MM":
      return (
        year +
        "." +
        zero(month) +
        "." +
        zero(day) +
        " " +
        zero(h) +
        ":" +
        zero(m)
      );
    case "YY/MM/DD HH:MM":
      return (
        year +
        "/" +
        zero(month) +
        "/" +
        zero(day) +
        " " +
        zero(h) +
        ":" +
        zero(m)
      );
    case "YY-MM-DD HH:MM":
      return (
        year +
        "-" +
        zero(month) +
        "-" +
        zero(day) +
        " " +
        zero(h) +
        ":" +
        zero(m)
      );
    case "YYMMDD-HHMM-SS":
      return (
        year +
        "" +
        zero(month) +
        "" +
        zero(day) +
        "-" +
        zero(h) +
        "" +
        zero(m) +
        "-" +
        zero(s)
      );
    case "YY-MM-DD":
      return year + "-" + zero(month) + "-" + zero(day);
    case "YY-MM":
      return year + "-" + zero(month);
    case "YYMMDD":
      return year + zero(month) + zero(day);
    case "ts":
      return ts;
    default:
      return year + "" + zero(month) + "" + zero(day);
  }
};

/**
 * 时间戳转日期
 * @param {String} types 转换类型
 * @param {String} timestamp 时间戳
 */
const timeStampConversion = (timestamp, types) => {
  // 补全为13位
  let arrTimestamp = (timestamp + "").split("");
  for (var start = 0; start < 13; start++) {
    if (!arrTimestamp[start]) {
      arrTimestamp[start] = "0";
    }
  }
  timestamp = arrTimestamp.join("") * 1;
  let date = new Date(timestamp);
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let hours = date.getHours();
  let minutes = date.getMinutes();
  let seconds = date.getSeconds();

  switch (types) {
    case "YY-MM-DD":
      return year + "-" + zero(month) + "-" + zero(day);
    case "HH:MM":
      return zero(hours) + ":" + zero(minutes);
    case "HH-MM":
      return zero(hours) + "-" + zero(minutes);
    case "HH:MM:SS":
      return zero(hours) + ":" + zero(minutes) + ":" + zero(seconds);
    case "HH:MM-HH:MM":
      return (
        zero(hours) +
        ":" +
        zero(minutes) +
        "-" +
        zero(hours) +
        ":" +
        zero(minutes)
      );
    case "MM月DD日":
      return zero(month) + "月" + zero(day) + "日";
    case "YY年MM月DD日":
      return year + "年" + zero(month) + "月" + zero(day) + "日";
    //YY-MM-DD HH:MM
    default:
      return (
        year +
        "-" +
        zero(month) +
        "-" +
        zero(day) +
        " " +
        zero(hours) +
        ":" +
        zero(minutes)
      );
  }
};

/**
 * 获取月份总天数
 * @param {String} year 2022
 * @param {String} month 09/9 
 * @returns 
 */
const getMonthTotalDay = (year, month)=>{
  var d = new Date(year, month, 0);
  return d.getDate();
}

//数值补0方法
const zero = (value) => {
  if (value < 10) return "0" + value;
  return value;
};

/**
 * 是否为空
 * @param {String | Object | Array} str
 * @returns
 */
const isEmpty = (str) => {
  return (
    str == null ||
    str == "" ||
    str == undefined ||
    str == "undefined" ||
    str.length == 0
    // JSON.stringify(str) == "{}"
  );
};

const writeLog = (log) => {
  const dir = "logs";
  //检测文件目录existsSync
  if (!fs.existsSync(dir)) {
    //创建文件目录
    fs.mkdirSync(dir);
  }
  const logName = `${dir}/${getCurrentTime("YYMMDD-HHMM-SS")}_log.txt`;
  fs.writeFile(logName, JSON.stringify(log), "utf-8", function (error) {
    if (error) {
      // console.log("文件写入数据失败...");
      return;
    }
    // console.log("文件写入数据成功...");
  });
};

module.exports = {
  getCurrentTime,
  timeStampConversion,
  isEmpty,
  writeLog,
  getMonthTotalDay,
  zero
};
